/**
 *  Copyright (c) 2014 The xmlepf project.  All rights reserved.
 *
 *  This file is part of xmlepf (Event Driven Testing Framework).
 *
 *  xmlepf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  xmlepf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with xmlepf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/xmlepf.
 */
package org.xmlepf.xtemp.functions;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.om.SequenceIterator;
import net.sf.saxon.tree.iter.SingleNodeIterator;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.SequenceType;
import org.xmlepf.xtemp.event.Event;
import org.xmlepf.xtemp.event.EventManager;
import org.xmlepf.xml.adapter.NodeSerializer;
import org.xmlepf.utils.ObjectCreator;
import org.xmlepf.xml.adapter.XMLNode;
import org.xmlepf.xtemp.script.ExecutionContext;

/**
 *
 *
 */
public class Post extends ExtensionFunctionDefinition {

    private static String catchLocalName = "post";
    private static String catchPrefix = "xtemp";
    private static String catchNameSpace =
            "http://docs.oasis-open.org/tamie/xtemp/200909";
    private static StructuredQName functionName =
            new StructuredQName(catchPrefix, catchNameSpace, catchLocalName);
    private static SequenceType[] argumentTypes = new SequenceType[]{SequenceType.SINGLE_NODE};
    private ExecutionContext executionContext;

    public Post(ExecutionContext executionContext) {
        this.executionContext = executionContext;
    }

    @Override
    public StructuredQName getFunctionQName() {
        return functionName;
    }

    @Override
    public int getMinimumNumberOfArguments() {
        return 1;
    }

    @Override
    public int getMaximumNumberOfArguments() {
        return 4;
    }

    @Override
    public SequenceType[] getArgumentTypes() {
        return argumentTypes;
    }

    @Override
    public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
        return SequenceType.EMPTY_SEQUENCE;
    }

    @Override
    public ExtensionFunctionCall makeCallExpression() {
        return new PostCall( executionContext );
    }

    private class PostCall extends ExtensionFunctionCall {

        private ExecutionContext executionContext;
        private EventManager eventManager;

        public PostCall(ExecutionContext executionContext) {
            this.executionContext = executionContext;
        }
        
        @Override
        public SequenceIterator call(SequenceIterator[] arguments, XPathContext context) throws XPathException {

            NodeInfo inputNode = (NodeInfo) arguments[0].next();
            
            try {
                NodeSerializer s = (NodeSerializer) ObjectCreator.get("NodeSerializer");
                String str = s.serialize( (XMLNode) inputNode);
                Event event = (Event) ObjectCreator.get("Event");
                event.initialize(str);
                event.setSource("post");
                eventManager.postEvent(event);
            } catch (Exception ex) {
                Logger.getLogger(QueryBoard.class.getName()).log(Level.SEVERE, null, ex);
            }

            return SingleNodeIterator.makeIterator(null);
        }
    }
}

